package com.mall.user.services;

import com.mall.user.IUserService;
import com.mall.user.constants.SysRetCodeConstants;
import com.mall.user.converter.UserConverterMapper;
import com.mall.user.dal.entitys.Member;
import com.mall.user.dal.persistence.MemberMapper;
import com.mall.user.dto.CheckAuthRequest;
import com.mall.user.dto.CheckAuthResponse;
import com.mall.user.dto.UserLoginRequest;
import com.mall.user.dto.UserLoginResponse;
import com.mall.user.utils.AESUtil;
import com.mall.user.utils.ExceptionProcessorUtils;
import com.mall.user.utils.JwtTokenUtils;
import lombok.extern.slf4j.Slf4j;
import org.apache.dubbo.config.annotation.Service;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import tk.mybatis.mapper.entity.Example;

import java.util.List;

/**
 * @author Liang Shunhao
 * @description: 用户服务层实现
 * @date 2021/9/28 23:11
 */
@Slf4j
@Component
@Service
public class UserServiceImpl implements IUserService {
    @Autowired
    MemberMapper memberMapper;

    @Autowired
    UserConverterMapper userConverterMapper;

    @Override
    public UserLoginResponse checkLogin(UserLoginRequest userLoginRequest) {
        // 对用户密码进行加密
        AESUtil aesUtil = new AESUtil(userLoginRequest.getPassword());
        String encrypt = aesUtil.encrypt();

        // 验证用户登录信息
        Example example = new Example(Member.class);
        example.createCriteria().andEqualTo("username", userLoginRequest.getUserName())
                .andEqualTo("password", encrypt);
        List<Member> users = memberMapper.selectByExample(example);
        // 密码验证不成功
        if (users.size() == 0) {
            return null;
        }

        // 邮箱验证

        // 密码验证成功
        Member member = users.get(0);
        // 对象转换
        UserLoginResponse converter = userConverterMapper.converter(member);
        // 添加token
        String token = JwtTokenUtils.builder().msg(converter.getUsername()).
                build().creatJwtToken();
        converter.setToken(token);
        return converter;
    }

    @Override
    public CheckAuthResponse validToken(CheckAuthRequest checkAuthRequest) {
        // 解析token
        String token = checkAuthRequest.getToken();
        String userinfo = JwtTokenUtils.builder().token(token).build().freeJwt();


        CheckAuthResponse checkAuthResponse = new CheckAuthResponse();
        checkAuthResponse.setUserinfo(userinfo);
        // 根据用户数据查询用户信息
        Example example = new Example(Member.class);
        example.createCriteria().andEqualTo("username", userinfo);
        List<Member> members = memberMapper.selectByExample(example);
        // 用户不存在
        if (members.size() == 0) {
            checkAuthResponse.setMsg("用户token无效");
            checkAuthResponse.setCode(SysRetCodeConstants.TOKEN_VALID_FAILED.getCode());
            return checkAuthResponse;
        }

        checkAuthResponse.setCode(SysRetCodeConstants.SUCCESS.getCode());
        return checkAuthResponse;
    }
}
